package com.jms.fu.model.bo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.struts.upload.FormFile;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.App;
import com.jms.fu.model.bean.Comment;
import com.jms.fu.model.dao.AppDAO;

public class AppBO {
	AppDAO appdao = null;
	public void updateApp(App app) throws DAOException {
		appdao=new AppDAO();
		appdao.updateApp(app);
	}
	
	public void updateFullApp(App app) throws DAOException {
		appdao=new AppDAO();
		appdao.updateFullApp(app);
	}
	
	
	public void deleteApp(int appId) throws DAOException{
		appdao=new AppDAO();
		appdao.deleteApp(appId);
	}
	public void writeFileToFolder(FormFile file,String path) throws IOException{
		FileOutputStream outputStream = null;
		File appfolder = new File(path);
		if (!appfolder.exists()) {
			appfolder.mkdirs();
		}
		File appfile = new File(path, file.getFileName());
		outputStream = new FileOutputStream(appfile);
		outputStream.write(file.getFileData());
		outputStream.flush();
		outputStream.close();
	}
	
	public boolean checkPath(String path) throws DAOException{
		appdao = new AppDAO();
		return appdao.checkPath(path);
	}
	public void uploadApp(App app) throws DAOException{
		appdao = new AppDAO();
		appdao.uploadApp(app);
	}
	public ArrayList<Comment> getCommentByAppId(int appId) throws DAOException{
		appdao = new AppDAO();
		return appdao.getCommentByAppId(appId);
	}
	
	public void addComment(int appId,String username,String comment) throws DAOException {
		appdao = new AppDAO();
		appdao.addComment(appId, username, comment);
	}
	
	public ArrayList<App> getListApp(String cat, String sortby,
			String pricefilter, int numberinpage, int pagenumber) throws DAOException {
		appdao = new AppDAO();
		return appdao.getListApp(cat, sortby,pricefilter, numberinpage,pagenumber);
	}
	public int getNumberOfPageApp(String cat,String pricefilter,int numberinpage) throws DAOException{
		appdao = new AppDAO();
		return appdao.getNumberOfPageApp(cat,pricefilter,numberinpage);
	}
	public App getAppByID(int appid) throws DAOException{
		appdao = new AppDAO();
		return appdao.getAppByID(appid);
	}
	public ArrayList<String> getNumberOfFile(String directoryName) {
	    File directory = new File(directoryName);
	    ArrayList<String> listf = new ArrayList<String>();
	    // get all the files from a directory
	    File[] fList = directory.listFiles();
	    if(fList == null){
	    	return listf;
	    }
	    for (File file : fList) {
	        if (file.isFile()) {
	           listf.add(file.getName());
	        }
	    }
	    return listf;
	}

	public ArrayList<App> getHotApp() throws DAOException {
		appdao = new AppDAO();
		return appdao.getHotApp();
	}
	public ArrayList<App> getAppBySearch(String search) throws DAOException {
		appdao = new AppDAO();
		return appdao.getAppBySearch(search);
	}
	public ArrayList<App> getAppDownByUser(String username) throws DAOException {
		appdao = new AppDAO();
		return appdao.getAppDownByUser(username);
	}
	public ArrayList<App> getAppByUser(String username) throws DAOException {
		appdao = new AppDAO();
		return appdao.getAppByUser(username);
	}

	public ArrayList<App> getNewApp() throws DAOException {
		appdao = new AppDAO();
		return appdao.getNewApp();
	}
}
